package uk.co.kevinlockwood.kol;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.lang.StringEscapeUtils;

import uk.co.kevinlockwood.kol.droid.DroidHelpers;
import uk.co.kevinlockwood.kol.droid.G;

public class JavaHelpers {
	/**
	 * Returns an Integer parsed from the given string, starting after 'start' characters and ending with 'end' (exclusive)
	 * @param string
	 * @param start
	 * @param end
	 * @return Integer
	 */
	public static Integer substringToInt(String s, String start, String end) {
		try {
			String sub = substringToString(s, start, end);
			if (sub != null) {
				return Integer.parseInt(sub.replaceAll(",", ""), 10);
			}
		} catch (Exception e) {
			DroidHelpers.trace("s=[",s,"] start=[",start,"] end=[",end,"]");
			DroidHelpers.printStackTrace(e);
		}
		return null;
	}

	/**
	 * Returns a String from the given string, starting after 'start' characters and ending with 'end' (exclusive)
	 * @param string
	 * @param start
	 * @param end
	 * @return String
	 */
	public static String substringToString(String s, String start, String end) {
		try {
			int startPos = 0;
			int endPos = s.length();
			if (start != null) {
				startPos = s.indexOf(start);
				if (startPos >= 0) startPos += start.length();
			}
			if (end != null) {
				endPos = s.indexOf(end,startPos);
			}
			if (startPos >= 0 && endPos >= 0) {
				return s.substring(startPos, endPos);
			}
		} catch (Exception e) {
			DroidHelpers.trace("s=[",s,"] start=[",start,"] end=[",end,"]");
			DroidHelpers.printStackTrace(e);
		}
		return null;
	}

	/**
	 * Returns the MD5 sum of the given string as a hex string
	 * @param str
	 * @return hex string
	 */
	public static String md5sum(String str) {
		StringBuilder s = new StringBuilder();
		try {
			MessageDigest md = MessageDigest.getInstance(G.MD5);
			byte[] hash = md.digest(str.getBytes(G.UTF8));

			int i;
			for (byte b : hash) {
				i = b & 0xff;
				if (i < 0x10) s.append("0"); // we might need a leading zero
				s.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException nsae) {
			// we know MD5 is a valid algorithm...
		} catch (UnsupportedEncodingException uee) {
			// ... and that UTF-8 is a valid encoding format
		}
		return s.toString();
	}

	/**
	 * Returns the URL encoded string of the given string
	 * @param str
	 * @return encoded string
	 */
	public static String urlencode(String str) {
		String s = null;
		try {
			s = URLEncoder.encode(str, G.UTF8);
		} catch (UnsupportedEncodingException e) {
			// we know that UTF-8 is a valid encoding format
		}
		DroidHelpers.trace("input str=[",str,"] encoded=[",s,"]");
		return s;
	}

	/**
	 * Returns the URL decoded string of the given string
	 * @param str
	 * @return decoded string
	 */
	public static String urldecode(String str) {
		String s = null;
		try {
			s = URLDecoder.decode(str, G.UTF8);
		} catch (UnsupportedEncodingException e) {
			// we know that UTF-8 is a valid encoding format
		}
		DroidHelpers.trace("input str=[",str,"] decoded=[",s,"]");
		return s;
	}

	/**
	 * Returns the URL encoded string of the given string
	 * @param str
	 * @return encoded string
	 */
	public static String htmlencode(String str) {
		String s = null;
		s = StringEscapeUtils.escapeHtml(str);
		DroidHelpers.trace("input str=[",str,"] encoded=[",s,"]");
		return s;
	}

	/**
	 * Returns the URL decoded string of the given string
	 * @param str
	 * @return decoded string
	 */
	public static String htmldecode(String str) {
		String s = null;
		s = StringEscapeUtils.unescapeHtml(str);
		DroidHelpers.trace("input str=[",str,"] decoded=[",s,"]");
		return s;
	}
}
